<template>
  <div class="add-field-wrap">
    <!-- :class="dataForm.isFx ? 'medium-common-dialog-size dialog-scroll' : 'small-common-dialog-size dialog-scroll'" -->
    <el-dialog
      v-loading="loading"
      :visible.sync="dialogVisible"
      :before-close="handleClose"
      :show-close="false"
      :append-to-body="true"
      class="order-setting-dialog medium-common-dialog-size"
      @closed="closeDialog"
    >
      <Title :title="infoData === null ? '新建基础字段': '编辑基础字段'" font-size="16px"/>
      <add-field-module ref="addFieldModule" :max-height="tableHeight" @success="handleSuccess" @cancle="handleClose" />
    </el-dialog>
  </div>
</template>
<script>
import Title from '@/components/Title';
import AddFieldModule from '../module/addField';
export default {
  name: 'AddFieldDialog',
  components: {
    AddFieldModule,
    Title
  },
  props: {
    fieldInfo: {
      type: Object,
      default: () => null
    }
  },
  data() {
    return {
      loading: false,
      dialogVisible: false,
      infoData: null,
      dataForm: {},
      tableHeight: ''
    };
  },
  methods: {
    handleSuccess() {
      this.$emit('success');
      this.dialogVisible = false;
    },
    handleClose() {
      this.dialogVisible = false;
    },
    closeDialog() {
      this.$emit('closeDialog');
      this.$refs.addFieldModule.clear();
    },
    openDialog(defaultCategoryId) {
      this.infoData = this.fieldInfo;
      const clientHeight = window.innerHeight;
      this.tableHeight = (clientHeight * 3) / 5;
      this.$nextTick(() => {
        if (defaultCategoryId || defaultCategoryId === 0) this.$refs.addFieldModule.dataForm.categoryId = defaultCategoryId;
        this.dataForm = this.$refs.addFieldModule.dataForm;
        if (this.fieldInfo !== null) {
          this.$refs.addFieldModule.fieldInfo = this.fieldInfo;
          this.$refs.addFieldModule.isCreate = false;
          this.$refs.addFieldModule.getFieldDetail(this.fieldInfo.id);
        } else {
          this.$refs.addFieldModule.isCreate = true;
        }
      });
    },
    // defaultCategoryId 如果有传，则 创建/编辑 时默认使用这个分类id
    dialogShow(defaultCategoryId = null) {
      this.dialogVisible = true;
      this.openDialog(defaultCategoryId);
    }
  }
};
</script>
<style lang="scss" scoped>

</style>
